package com.shenzhen.horselive.utils;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {

    /**
     * 将毫秒转时分秒
     *
     * @param time
     * @return
     */
    public static String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        return hours > 0 ? String.format("%02d小时%02d分%02d秒", hours, minutes, seconds) : String.format("%02d分%02d秒", minutes, seconds);
    }
    /**
     *
     * @param mss  要转换的毫秒数
     * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
     * @author fy.zhang
     */
    public static String formatDuring(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
//        return days;
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return days + " 天 " + hours + " 小时 " + minutes + " 分 "
                + seconds + " 秒 ";
    }


    /**
     * 获取天数
     * @param mss
     * @return
     */
    public static  long getDay(long mss )
    {
        long days = mss / (1000 * 60 * 60 * 24);
        return days;
    }

    /**
     * 获取小时
     * @param mss
     * @return
     */
    public static  long  getHH(long mss )
    {
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        return hours;
    }

    /**
     * 获取分钟
     * @param mss
     * @return
     */
    public static  long  getmm(long mss )
    {
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        return minutes;
    }


    /**
     * 获取秒
     * @param mss
     * @return
     */
    public static  long  getss(long mss )
    {
        long seconds = (mss % (1000 * 60)) / 1000;
        return seconds;
    }
    /**
     * 将字符串型日期转换成日期
     *
     * @param dateStr    字符串型日期
     * @param dateFormat 日期格式
     * @return
     */
    public static Date stringToDate(String dateStr, String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
            return formatter.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 时间间隔
     * @param date  开始时间
     * @param date1   结束时间
     * @return
     */
    public static long  betweenDate(String   date,String  date1){
              String   formatstr = "yyyy.MM.dd HH:mm:ss";
              Date  starTime=  stringToDate(date,formatstr);
              Date  endTime =stringToDate(date1,formatstr);
              if (starTime!=null&&endTime!=null)
              return endTime.getTime()-starTime.getTime();
              else
                  return 0;

    }


    /**
     * 时间间隔
     * @param date  结束时间
     * @return
     */
    public static long  betweenNowDate(String   date){
        String   formatstr = "yyyy.MM.dd HH:mm:ss";
        Date  endTime =stringToDate(date,formatstr);
        if (endTime!=null)
            return endTime.getTime()-System.currentTimeMillis();
        else
            return 0;

    }







}
